package com.mo2323.merchant.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.mo2323.common.VO;
import com.mo2323.common.VOList;
import com.mo2323.common.util.AjaxObject;
import com.mo2323.service.merchant.IParkingOfferInputService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 28876
 */
@Controller
@RequestMapping("/Parking")
public class ParkingOfferInputController extends BaseController {

    @Reference
    IParkingOfferInputService parkingOfferInputService;


    @RequestMapping(value = "/getUserHistoryCarInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getUserHistoryCarInfo(@RequestParam String unionId, @RequestParam String inputCode, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        try {
            List<VO> historyCarInfo = parkingOfferInputService.getUserHistoryCarInfo(unionId, inputCode);
            return AjaxObject.ok().put("data", VOList.I(historyCarInfo).getData());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/getCarInfoAndMerchantInfoByFuzzy", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getCarInfoAndMerchantInfoByFuzzy(@RequestParam String unionId, @RequestParam String inputCode, @RequestParam String carNo, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        try {
            JSONObject jsonObject = parkingOfferInputService.getCarInfoAndMerchantInfoByFuzzy(unionId, inputCode, carNo);
            return AjaxObject.ok().put("data", jsonObject);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/updateMerchantAndCard", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject updateMerchantAndCard(@RequestParam String unionId, @RequestParam String inputCode, @RequestParam String carNo, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        try {
            parkingOfferInputService.updateMerchantAndCard(unionId, inputCode, carNo);
            return AjaxObject.ok();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxObject.error(e.getMessage());
        }
    }
}
